package com.xd.clap.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.clap.mapper.AppUpdateMapper;
import com.xd.clap.entity.AppUpdate;
import com.xd.clap.entity.AppUpdateVO;
import com.xd.clap.services.IAppUpdateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘唐武
 *
 * @since 2022-02-12
 */
@Slf4j
@Service
public class AppUpdateServiceImpl extends ServiceImpl<AppUpdateMapper, AppUpdate> implements IAppUpdateService {

    @Autowired
    private AppUpdateMapper appUpdateMapper;


    /**
     * app更新
     * @params [name, version]
     * @return com.yqs.pojo.AppUpdateVO
     * @Author: 刘唐武
     * @Date: 2022/2/12 16:41
     */
    @Override
    public AppUpdateVO checkUpdate(String appName, String version) {
//        AppUpdate appUpdate = appUpdateMapper.selectOne(new QueryWrapper<AppUpdate>().eq("app_name",appName));
        QueryWrapper<AppUpdate> queryWrapper = new QueryWrapper<>();
// 不添加任何条件，所以queryWrapper将是“空的”
        AppUpdate appUpdate = appUpdateMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(appUpdate) || appUpdate.getVersion().equals(version)){
            return new AppUpdateVO().setUpdate(false);
        }

        //1.0.0
        String s = version.replaceAll("\\.", "");
        System.out.println(s);
        System.out.println("---------------------------");
        System.out.println(s);
        if (appUpdate.getVersion().compareTo(version) <= 0){
            return new AppUpdateVO().setUpdate(false);
        }else {
            AppUpdateVO appUpdateVO = new AppUpdateVO()
                    .setUpdate(true)
                    .setWgtUrl(appUpdate.getWgtUrl())
                    .setPkgUrl(appUpdate.getPkgUrl());
            log.info("【检查更新】成功-----------------------数据：{}", appUpdateVO);
            return appUpdateVO;
        }
    }
}
